<template>
    <div>
        <h1>评论列表</h1>
        <div class="find-div">
            <el-input v-model="courseNumber" placeholder="通过课程号查询"></el-input>
            <el-button type="primary" @click="findCourseByCourseName">查询</el-button>
            <div>
                <el-table :data="commentList" border="true" >
                <el-table-column prop="id" label="评论Id" width="100">
                </el-table-column>
                <el-table-column prop="userId" label="用户ID" width="150">
                </el-table-column>
                <el-table-column prop="content" label="评论内容" width="200">
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="200">
                </el-table-column>
                <el-table-column prop="likeCount" label="点赞数" width="100">
                </el-table-column>
                <el-table-column prop="dislikeCount" label="点踩数" width="100">
                </el-table-column>
                <el-table-column label="编辑" >
                    <template slot-scope="scope">
                        <el-button @click="deleteComment(scope.row)" type="danger">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            courseNumber:"",
            commentList:[
                
            ]
        }
    },
    methods:{
        findCourseByCourseName(){
            this.$axios.get("http://localhost:8000/comment/adminFindByCourseNumber", {
                params:{
                    courseNumber:this.courseNumber
                },
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
            })
                .then(res => {
                    console.log(res)
                    this.commentList = res.data
                })
                .catch(err => {
                    console.log(err)
                    this.$message.error(err.response.data.message)
                })
        },
        deleteComment(comment){
            this.$axios.delete("http://localhost:8000/comment/deleteComment/"+comment.id, {
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
            })
                .then(res => {
                    console.log(res)
                    this.$message.success("删除成功")
                    this.$router.go(0) 
                })
                .catch(err => {
                    console.log(err)
                    this.$message.error("删除失败")
                })
        }
    }
}
</script>

<style scoped lang="scss">
.find-div {

    .el-input {
        width: 500px
    }
    .el-table{
        width:80%
    }
}
</style>